//
//  MHCommentCell.m
//  MHO
//
//  Created by papo on 4/2/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHCollectionCell.h"
#import "Collection.h"
#import "Lib.h"
//#im

@implementation MHCollectionCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setCollection:(Collection *)collection {
    _collection = collection;
    
    _collectionNameLb.text = collection.collectionName;
    _viewsCountLb.text = [NSString stringWithFormat:@"Lượt xem: %@",collection.views];
    
    if (collection.colletionId.intValue == 0) {
        self.removeButton.hidden = YES;
    }
    else {
        self.removeButton.hidden = NO;
    }
    
}

- (IBAction)deleteCollection:(id)sender {
    [UIAlertView showWithTitle:@"Thông báo" message:@"Bạn có chắc muốn xoá chủ đề này không?" cancelButtonTitle:@"Không" otherButtonTitles:@[@"Có"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [Lib showLoadingView];
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [Collection deleteCollection:_collection];
               dispatch_async(dispatch_get_main_queue(), ^{
                   [Lib removeLoadingView];
                   [[NSNotificationCenter defaultCenter] postNotificationName:@"MHCollectionCellDelete" object:nil];
                   [UIAlertView showWithTitle:@"Thông báo" message:@"Đã xoá thành công!" cancelButtonTitle:@"OK" otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                       
                   }];
               });
            });
        }
    }];
}

+ (MHCollectionCell*)cellFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"MHCollectionCell" owner:self options:nil] objectAtIndex:0];
}

- (void)setListGame:(NSArray *)list {
    int i = 0;
    for (; i < 6; i++) {
        UIImageView* imgView = (UIImageView*)[self.contentView viewWithTag:10 + i];
        
        if (i < list.count) {
            Application* app = list[i];
            [imgView setImageWithURL:[Contants getImageUrl:app.imagePath width:100 height:100]];
            imgView.hidden = NO;
        }
        else {
            imgView.hidden = YES;
        }
    }
}
@end
